//
//  ZZOrderView.m
//  ZuZuBao
//
//  Created by Felix on 2024/4/11.
//

#import "ZZOrderView.h"
#import "ZZOrderTableViewCell.h"

@interface ZZOrderView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * dataArray;

@end

@implementation ZZOrderView
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [NSMutableArray array];
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavBarHeight - 40) style:UITableViewStylePlain];
//        tableView.backgroundColor = kBackColor;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:self.tableView];
        [self.tableView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
    }
    return self;
}

- (void)setViewData:(NSInteger)index {
    // 请求数据
    if (index == 0) {
        [self getData:0];
    } else if (index == 1) {
        [self getData:1];
    } else if (index == 2) {
        [self getData:2];
    } else if (index == 3) {
        [self getData:3];
    }
}

-(void)getData:(NSInteger)count {
    NSString *urlString = [NSString stringWithFormat:@"%@%@", kBaseUrl, kQryGoods];
    NSDictionary *params = @{@"current": @(1),
                                 @"gameId" : @(4),
                                 @"labelType": @(4),
                                 @"size": @(20),
                                 @"synthesizeSort": @(1)
                                 };
    
    [kFFNetManager POST:urlString parameters:params headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            NSDictionary * reponseDic = responseObject[@"result"];
            [self.dataArray removeAllObjects];
            NSArray * array = reponseDic[@"record"];
            NSMutableArray * mutArr = [NSMutableArray array];
            for (int i = 0; i < array.count; i++) {
                NSDictionary * dic = array[i];
                ZZProductModel * model = [[ZZProductModel alloc] init];
                model = [ZZProductModel mj_objectWithKeyValues:dic];
                [mutArr addObject:model];
            }
            if (mutArr.count > count) {
                ZZProductModel * model = mutArr[count];
                [self.dataArray addObject:model];
            }

            [self.tableView reloadData];
        }else {
             
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 返回你想要的行数
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}
  
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 注册自定义cell的类，如果还没有注册过
    static NSString *cellIdentifier = @"ZZOrderTableViewCell";
    [tableView registerClass:[ZZOrderTableViewCell class] forCellReuseIdentifier:cellIdentifier];
      
    // 从队列中复用cell或创建新的cell
    ZZOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    // 设置cell的数据
    [cell setCellData:self.dataArray[indexPath.row]];
    
    return cell;
}


@end
